package com.qfedu.microuser.pointrule.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qfedu.common.core.entity.PointRule;
import com.qfedu.common.redis.utils.RedisCache;
import com.qfedu.microuser.pointrule.mapper.PointRuleMapper;
import com.qfedu.microuser.pointrule.service.PointRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Classname PointRuleServiceImpl
 * @Description TODO
 * @Date 2025-09-24 14:49
 * @Created by 老任与码
 */
@Service
public class PointRuleServiceImpl implements PointRuleService {
    @Resource
    private PointRuleMapper pointRuleMapper;

    @Resource
    private RedisCache redisCache;

    @Override
    public PointRule ruleInfoByType(Integer pointType) {
        return pointRuleMapper.selectOne(Wrappers.lambdaQuery(PointRule.class)
                .eq(PointRule::getPointType, pointType));
    }

    // 先从缓存中取数据，如果没有从数据库查，然后将数据写入缓存
    @Override
    public List<PointRule> ruleList() {

        List<PointRule> list = redisCache.getCacheObject("point:rule");
        if (list == null) {
            list = pointRuleMapper.selectList(null);
            redisCache.setCacheObject("point:rule", list, 24 * 3600, TimeUnit.SECONDS);
        }

        return list;
    }
}
